﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using yncTech.BusinessObjects;
using System.Data;
using System.Collections;
using EntitySpaces.Interfaces;


public partial class Admin_FinLend_FinLend003a : AdminBasePage
{
    protected decimal? lendId;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["lendId"] != null)
        {
            lendId = decimal.Parse(Request.QueryString["lendId"]);
        }

        if (!IsPostBack)
        {

        }
    }


    protected void PageChanged(object sender, DataGridPageChangedEventArgs e)
    {


        FinReceiptQuery q1 = new FinReceiptQuery("q1");


        TSCodeTableQuery q2 = new TSCodeTableQuery("q2");
        TSCodeTableQuery q3 = new TSCodeTableQuery("q3");
        TSCodeTableQuery q4 = new TSCodeTableQuery("q4");
        TSCodeTableQuery q6 = new TSCodeTableQuery("q6");
        BFThirdUnitQuery q5 = new BFThirdUnitQuery("q5");
        BFCompanyDicQuery q7 = new BFCompanyDicQuery("q7");
        VReceiptExtInfoQuery q8 = new VReceiptExtInfoQuery("q8");

        TSCodeTableQuery q9 = new TSCodeTableQuery("q9");
        FinBoxQuery q10 = new FinBoxQuery("q10");

        FinLendReceiptQuery q11 = new FinLendReceiptQuery("q11");



        q1.LeftJoin(q9).On(q1.VoucherDescription == q9.Code);

        q1.LeftJoin(q2).On(q1.FunctionDept == q2.Code);
        q1.LeftJoin(q3).On(q1.StorageStatus == q3.Code);
        q1.LeftJoin(q4).On(q1.VoucherDescription == q4.Code);
        q1.LeftJoin(q5).On(q1.StorageLocation == q5.ThirdUnitID);
        q1.LeftJoin(q6).On(q1.Division == q6.Code);
        q1.LeftJoin(q7).On(q1.CompanyCode == q7.CompanyCode);
        q1.LeftJoin(q8).On(q1.ReceiptID == q8.ReceiptID);
        q1.InnerJoin(q11).On(q1.ReceiptID == q11.ReceiptID);

        q1.SelectAll().Select(q1.CreatedDttm, q2.Value1.As("FunctionDeptStr"), q3.Value1.As("StorageStatusStr"), q4.Value1.As("VoucherDescriptionName"), q5.Name.As("StorageLocationStr"), q6.Value1.As("DivisionStr"), q7.CompanyName, q7.CompanyCode, q8.SupplierBoxNo, q8.BoxNo, q9.Value1.As("DocDesc"));

        ArrayList args = new ArrayList();
        args.Add(q11.LendID == lendId.Value);
        q1.Where(args.ToArray()).OrderBy(q1.DocumentsNo.Ascending);

        DataTable tableSorce = q1.LoadDataTable();
        pageGrid.DataSource = tableSorce;
        pageGrid.CurrentPage = e.NewPageIndex;
        pageGrid.DataBind();
    }

    protected int getYear(object StoreStartDate)
    {
        int result = 0;

        if (StoreStartDate != null && IsDateTime(StoreStartDate.ToString()))
        {
            result = DateTime.Today.Year - 1 - DateTime.Parse(StoreStartDate.ToString()).Year;
            if (result < 0)
            { result = 0; }
        }

        return result;
    }


    private bool IsDateTime(string str)
    {
        DateTime temp;
        return DateTime.TryParse(str, out temp);
    }


    protected void btnSave_Click(object sender, EventArgs e)
    {
        FinLend finLend = new FinLend();
        if (finLend.LoadByPrimaryKey(lendId.Value))
        {
            finLend.LendStatus = "LendStatus004";
            ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(), "showNews('申请已批准！');", true);
        }
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        FinLend finLend = new FinLend();
        if (finLend.LoadByPrimaryKey(lendId.Value))
        {
            finLend.LendStatus = "LendStatus005";
            ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(), "showNews('申请已拒绝！');", true);
        }
    }

    protected void btnRetuen_Click(object sender, EventArgs e)
    {
        Response.Redirect("FinLend003.aspx");
    }

}